package com.wc.alorithm_luogu._贪心.P1049;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Arrays;

/**
 * @Author congge
 * @Date 2023/9/30 11:16
 * @description 装箱问题
 * https://www.luogu.com.cn/problem/P1049
 */
public class Main {
    public static void main(String[] args) {
        Input in = new Input();
        int v = in.nextInt();
        int n = in.nextInt();
        int[] boxes = new int[n];
        /**
         * dp[i] 表示当前i容量能装下的最大体积
         */
        int[][] dp = new int[n][v + 1];

        for (int i = 0; i < n; i++) {
            boxes[i] = in.nextInt();
        }
        Arrays.sort(boxes);
        for (int i = boxes[0]; i <= v; i++) {
            dp[0][i] = boxes[0];
        }

        /**
         * 背包问题
         */
        for (int i = 1; i < n; i++) {
            for (int j = 0; j <= v; j++) {
                if (j >= boxes[i]) {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - boxes[i]] + boxes[i]);
                } else {
                    dp[i][j] = dp[i - 1][j];
                }
            }
        }

        System.out.println(v - dp[n - 1][v]);

    }

    static class Input {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        public int nextInt() {
            try {
                in.nextToken();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return (int) in.nval;
        }
    }
}
